﻿using Instagram.Version2.Helper;
using Instagram.Version2.Models;
using System;
using System.Linq;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

// The Content Dialog item template is documented at http://go.microsoft.com/fwlink/?LinkID=390556

namespace Instagram.Version2
{
    public sealed partial class Login : Page
    {
        public Login()
        {
            this.InitializeComponent();
        }

        async private void Login_Click(object sender, RoutedEventArgs args)
        {
            if (email.Text.Length > 3 && password.Password.Count() > 3)
            {
                var user = new UserModel
                {
                    Email = email.Text,
                    Password = password.Password,
                    RememberMe = false
                };
                user = await ApiCaller.PostAsync<UserModel>("api/Account/Login", user);

                if (user.Name != null)
                {
                    GlobalVariables.User = user.Name;
                    this.Frame.Navigate(typeof(MainPage), user);
                }
                else
                {
                    body.Text = "Check login or password.";
                }
            }
            else
            {
                body.Text = "Fill all fields.";
            }
        }

        private async void Register_Click(object sender, RoutedEventArgs args)
        {
            Frame.Navigate(typeof(RegisterPage));
        }
    }
}
